import logger from '../core/logger';
import apiInfo from './core/apiInfo';
import apiProvider from './core/apiProvider';
import history from '../model/other/history';
import { clientBase } from './core/clientBase';
import returnModel from '../model/common/returnModel';

class historyClient extends clientBase {
  constructor() {
    super();
  }

  getHistory(pageNum: number): Promise<returnModel<history>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        var info = new apiInfo("http://api.bilibili.com/x/v2/history", `&pn=${pageNum}&ps=20`, null, true);
        info.buildVer = apiProvider.Build6;
        this.client.getResult(info).then((res) => {
          if (res.code == '0') {
            var data: history = res;
            resolve(self.buildSuccessResult<history>(data));
          } else {
            resolve(self.buildFailedResult<history>(res.toString()));
          }
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<history>(e));
      }
    });
  }

  clear(id: string): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        var info = new apiInfo("http://api.bilibili.com/x/v2/history/delete", `&kid=${id}`, null, true);
        info.buildVer = apiProvider.Build6;
        this.client.getResult(info).then((res) => {
          if (res.code == '0')
            resolve(self.buildSuccessResult<any>());
          else
            resolve(self.buildFailedResult<any>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<any>(e));
      }
    });
  }

  clearAll(): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        var info = new apiInfo("http://api.bilibili.com/x/v2/history/clear", '', null, true);
        info.buildVer = apiProvider.Build4;
        this.client.getResult(info).then((res) => {
          if (res.code == '0')
            resolve(self.buildSuccessResult<any>());
          else
            resolve(self.buildFailedResult<any>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<any>(e));
      }
    });
  }
}
export default new historyClient();